package com.qk.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.dto.CourseDto;
import com.qk.entity.Course;
import com.qk.mapper.CourseMapper;
import com.qk.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CourseServiceImpl implements CourseService {

    @Autowired
    private CourseMapper courseMapper;

    @Override
    public PageResult<Course> getList(CourseDto courseDto) {
        //1.设置分页参数,调用pageHelper中的page.Starter方法
        PageHelper.startPage( courseDto.getPage(),courseDto.getPageSize());
        //2.调用mapper查询
        List< Course > courseList = courseMapper.selectList(courseDto);
        //3.强转类型为page,调用page方法
        Page<Course> page= (Page< Course>) courseList;
        //4.响应结果返回
        return new PageResult<>(page.getTotal(),page.getResult());
    }

    @Override
    public void deleteCourse(Integer id) {
        courseMapper.deleCourseById(id);
    }

    @Override
    public void addCourse(Course course) {
        //补全基本属性
        course.setCreateTime(LocalDateTime.now());
        course.setUpdateTime(LocalDateTime.now());
        courseMapper.addCourse(course);
    }

    @Override
    public Course getCourseById(Integer id) {
       return courseMapper.selectById(id);
    }

    @Override
    public void updateCourse(Course course) {
        //补全基本属性
        course.setUpdateTime(LocalDateTime.now());
        courseMapper.updateCourse(course);
    }

    @Override
    public List<Course> getAll() {
        return courseMapper.getAll();
    }

    @Override
    public List<Course> getCourseBySubject(String subject) {
        return courseMapper.selectCourseBySubject(subject);
    }
}
